<template>
  <common-info-dialog
    :visible.sync="dialogShow"
    @cancel="close"
    :title="dialogTitle"
    :infoLoading="infoLoading"
  >
    <common-info-table>
      <common-info-table-cell label="用户名" width="50%"
        >{{ detail.userName }}
      </common-info-table-cell>
      <common-info-table-cell label="姓名" width="50%"
        >{{ detail.nickName }}
      </common-info-table-cell>
      <common-info-table-cell label="所属角色" width="50%"
        >{{ detail.roleNames }}
      </common-info-table-cell>
      <!-- <common-info-table-cell label="数据权限" width="50%">{{
        detail.authNames
        }}
      </common-info-table-cell> -->
      <!--<common-info-table-cell label="用户来源" width="50%">
        <span v-if="detail.userSource === 1">自建用户</span>
        <span v-else-if="detail.userSource === 2">从外系统同步</span>
      </common-info-table-cell>-->
      <!--      <common-info-table-cell label="生效日期" width="50%">{{detail.effTime}}</common-info-table-cell>-->
      <!--      <common-info-table-cell label="失效日期" width="50%">{{detail.expTime}}</common-info-table-cell>-->
      <common-info-table-cell label="手机号" width="50%"
        >{{ detail.phone }}
      </common-info-table-cell>
      <common-info-table-cell label="身份证号" width="50%"
        >{{ detail.identity }}
      </common-info-table-cell>

      <common-info-table-cell label="性别" width="50%">
        <span v-if="detail.sex == '0'">男</span>
        <span v-else-if="detail.sex == '1'">女</span>
      </common-info-table-cell>
      <common-info-table-cell label="辅助策略"
        >{{ detail.userSecondaryLoginPolicy }}
      </common-info-table-cell>
      <common-info-table-cell label="邮箱" width="100%"
        >{{ detail.email }}
      </common-info-table-cell>
      <!--      <common-info-table-cell label="辅助策略" width="50%">{{userSecondaryLoginPolicy}}</common-info-table-cell>-->
      <!-- <common-info-table-cell label="密码" width="50%" v-if="!form.userId">
         <span v-if="detail.genPwdWay == 1">默认密码</span>
         <span v-else-if="detail.genPwdWay == 2">随机密码</span>
       </common-info-table-cell>-->
      <common-info-table-cell label="所属组织"
        >{{ detail.deptFullName }}
      </common-info-table-cell>
      <common-info-table-cell label="备注"
        >{{ detail.remark }}
      </common-info-table-cell>
    </common-info-table>
  </common-info-dialog>
</template>

<script>
import { user_info } from "@/api/management/usercenter/user.js";
import Utils from "@/utils";
import { decode } from "@/utils/aesUtils";

export default {
  props: {
    loginPolicySelect: {
      type: Array,
      default: () => [],
    },
    authSelect: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      dialogTitle: "用户详情",
      dialogShow: false,
      detail: {},
      infoLoading: false,
    };
  },
  methods: {
    /**
     * 关闭
     */
    close() {
      this.dialogShow = false;
    },
    /**
     * 显示
     */
    show(data) {
      this.dialogShow = true;
      this.dialogTitle = "用户详情";
      if (!!data.userId) this.getUserInfo(data.userId);
    },
    getUserInfo(id) {
      this.infoLoading = true;
      user_info({ userId: id }).then((res) => {
        if (res && res.code === 200) {
          this.detail = res.data.userDeatailVo.user;

          this.detail.phone = Utils.phoneEncryption(
            decode(this.detail.phonenumber)
          );
          this.detail.identity = this.detail.identityCard
            ? Utils.identityEncryption(decode(this.detail.identityCard))
            : null;
          this.detail.email = this.detail.email
            ? Utils.emailEncryption(decode(this.detail.email))
            : "";
          this.detail.userSecondaryLoginPolicy =
            this.detail.userSecondaryLoginPolicyId &&
            this.loginPolicySelect.filter(
              (f) => f.policyType === this.detail.userSecondaryLoginPolicyId
            ).length > 0
              ? this.loginPolicySelect.filter(
                  (f) => f.policyType === this.detail.userSecondaryLoginPolicyId
                )[0].policyName
              : "";
          this.detail.roleNames = this.detail.roles
            .reduce((r, c) => {
              r.push(c.roleName);
              return r;
            }, [])
            .join(",");
          this.detail.deptFullName = this.detail.deptFullNameArr.join(",");
          this.detail.authNames = this.detail.permissionIds
            .reduce((r, c) => {
              if (
                res.data.loginUserPermissions.find((f) => f.permissionId === c)
              ) {
                r.push(
                  res.data.loginUserPermissions.find(
                    (f) => f.permissionId === c
                  ).permissionName
                );
              }
              return r;
            }, [])
            .join(",");
          this.infoLoading = false;
        } else {
          this.$GlobalErrorConfirm("获取信息失败", res.msg, "重新加载").then(
            () => {
              this.getUserInfo(id);
            }
          );
        }
      });
    },
    getLoginPolicy(val) {
      // return this.loginPolicySelect.filter(f => f.policyType === )
    },
  },
};
</script>
